﻿@{
    ViewData["Title"] = "设置角色权限";
    Layout = "_Edit";
}
<article class="page-container">
    <form action="" method="post" class="form form-horizontal" id="form-add">
       <input type="hidden" class="input-text" value="@ViewData["id"]" id="id" name="id">
        <div class="row cl">
            <div class="formControls">
                @foreach (var info in Model)
                {
                    <dl class="permission-list">
                        <dt>
                            <label>
                                @if (info.@checked)
                                {
                                    <input type="checkbox" value="@info.id" checked="checked" name="role-@info.id">
                                }
                                else
                                {
                                    <input type="checkbox" value="@info.id" name="role-@info.id">
                                }
                                @info.name
                            </label>
                        </dt>
                        @if (info.children != null && info.children.Count > 0)
                        {
                            @foreach (var ch in info.children)
                            {
                                <dd>
                                    <label class="">
                                        @if (ch.@checked)
                                        {
                                            <input type="checkbox" value="@ch.id" checked="checked" name="role-@ch.id">
                                        }
                                        else
                                        {
                                            <input type="checkbox" value="@ch.id" name="role-@ch.id">
                                        }
                                        @ch.name
                                    </label>
                                </dd>
                            }
                        }
                    </dl>
                }
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <button type="button" class="btn btn-success radius" onclick="operateFunction()" id="admin-role-save" name="admin-role-save"><i class="icon-ok"></i> 确定</button>
            </div>
        </div>
    </form>
</article>

@section javascript {
<script type="text/javascript">
          $(function(){
          $(".permission-list dt input:checkbox").click(function(){
              $(this).closest("dl").find("dd input:checkbox").prop("checked",$(this).prop("checked"));
          });
          $(".permission-list dd input:checkbox").click(function(){
              if($(this).prop("checked")){
                  $(this).closest("dl").find("dt input:checkbox").prop("checked",true);
              }
              else{
                  var l=$(this).closest(".permission-list").find("dd").find("input:checked").length;
                  alert(l);
                  if(l==0){
                     $(this).closest("dl").find("dt input:checkbox").prop("checked",false);
                  }
              }
          });
      });
    function operateFunction(){
             SubmitFormForAjax("/admin/setrolefunction", $("#form-add").serialize(), function (data) {
                 if (data.result) {
                         popupAlert('提交成功',1,function(){closePageLayer(true);});
                 }else{
                     popupAlert(data.message,2,function(data){layer.close(data);});
                 }
             },true);
          }
</script>
}

